package simplenotes;

import java.awt.Color;
import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class TaskRenderer extends javax.swing.JPanel implements ListCellRenderer {

    public TaskRenderer() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(jLabel1.getFont().deriveFont((jLabel1.getFont().getStyle() | java.awt.Font.ITALIC) & ~java.awt.Font.BOLD));
        jLabel1.setText("01.05.2009");

        jLabel2.setText("Сделать то-то и то-то...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        TaskObj ta = (TaskObj) value;

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        jLabel1.setText("до " + sdf.format(ta.getFinishTime().getTime()));

        String txt = ta.getTaskText();
        String txt1 = "";
        for (int i = 0; i < txt.length(); i++) {
            if (txt.codePointAt(i) == 10) {
                txt1 = txt1 + "...";
                break;
            } else {
                txt1 = txt1 + txt.substring(i, i + 1);
            }
        }
        jLabel2.setText(txt1);

        Calendar cc = new GregorianCalendar();
        cc.set(Calendar.HOUR_OF_DAY, 0);
        cc.set(Calendar.MINUTE, 0);
        cc.set(Calendar.SECOND, 0);
        cc.set(Calendar.MILLISECOND, 0);
        if (cc.after(ta.getFinishTime())) {
            jLabel2.setForeground(Color.RED);
        } else {
            if (ta.getTekuchka()) {
                jLabel2.setForeground(Color.BLUE);
            } else {
                jLabel2.setForeground(javax.swing.UIManager.getDefaults().getColor("Label.foreground"));
            }
        }

        if (isSelected) {
            setBackground(list.getSelectionBackground());
        } else {
            setBackground(javax.swing.UIManager.getDefaults().getColor("List.background"));
        }

        return this;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
